<?php


namespace App\Http\Service\Impl;


use App\Http\Service\IGoods;

use App\Http\Service\IIndex;

use app\Request;

use Hashids\Hashids;

use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;

use phpDocumentor\Reflection\Types\Array_;

use Ramsey\Uuid\Uuid;


use function foo\func;


class GoodsImpl implements IGoods

{

    function goodslist($request)
    {
        wlog($request->toArray(), 2);
        $where  = " where 1=1  ";
        $where1 = "";
        if ($request->lsh) {
            $where .= " and lsh like '%$request->lsh%'";
        }
        if ($request->bxr) {
            $where .= " and bxr like '%$request->bxr%' ";
        }
        if ($request->bxrbmmc) {
            $where .= " and bxrbmmc= '$request->bxrbmmc' ";
        }
        if ($request->cclx) {
            $where .= " and cclx like '%$request->cclx%' ";
        }
        if ($request->rwdh) {
            $where .= " and rwdh like '%$request->rwdh%'";
        }
        if ($request->gh) {
            $where .= " and gh = '$request->gh' ";
        }
        if ($request->sfsc == '是') {
            $where .= " and t.pzbh!= '' ";
        } else {
            $where .= " and t.pzbh is NULL";
        }
        if ($request->sqsj) {
            $where .= " and CAST(sqsj AS DATE) = '$request->sqsj' ";
        }
        if (!$request->lsh && !$request->bxr && !$request->bxrbmmc && !$request->cclx && !$request->rwdh && !$request->gh && !$request->bxrbmbm) {
            //$where .= " ORDER BY RowNum OFFSET ? ROWS  FETCH NEXT ? ROWS ONLY ";
        }
        if ($request->order) {
            $order = $request->order;
        } else {
            $order = "desc";
        }
        //ROW_NUMBER() OVER (ORDER BY a.lsh) AS RowNum
        $sql = "SELECT * from (
                                select convert(varchar(10),getdate(),120) zdrq,
                    '记' pzlb,
                    '0000' gdpzbh,
                    'deme' zdr,
                    a.lsh lsh,
                    h.ReceiveAt sqsj,
                    hh.FinishAt cwshsj,
                    j.personid gh,
                        a.报销人 bxr,
                        l.cdepcode bxrbmbm,
                        l.cdepname bxrbmmc,
                        a.出差类型 cclx,
                        a.出差开始 ccks,
                        a.出差结束 ccjs,
                        a.任务单号 rwdh,
                        a.tmp2  cwzzrwdh,
                        [dbo].[获取财务科目编码](a.出差类型,'城际交通费') clfbm,
                        sum(isnull(b.金额,0)) +
                        sum(isnull(c.交通费,0)) +
                        case when a.经理汇总='否'  then sum(isnull(d.食宿费小计,0))
                                else isnull(g.食宿费小计2,0) end +
                        isnull(e.国内补贴,0)+
                        isnull(e.国外补贴,0)+
                        isnull(e.多国补贴,0)+
                        isnull(e.误餐补贴,0)+
                        isnull(ffff.费用金额,0)+
                        isnull(f.费用金额,0)+isnull(fffff.费用金额,0)+isnull(fffffff.费用金额,0)+isnull(gg.费用金额,0) gsclf,
                        
                        [dbo].[获取财务科目编码](a.出差类型,'话费补贴') hfbtbm,
                              isnull(e.话费补贴,0) hfbt,
                        [dbo].[获取财务科目编码](a.出差类型,'招待费') zdfbm,
                        isnull(ff.费用金额,0)+isnull(ffffffff.费用金额,0) zdf,
                        [dbo].[获取财务科目编码](a.出差类型,'快递费') yfbm,
                        isnull(fffffffff.费用金额,0)  yf,
                        [dbo].[获取财务科目编码](a.出差类型,'护照费') hzfbm,
                        isnull( ffffff.费用金额,0) hzf,
                        [dbo].[获取财务科目编码](a.出差类型,'现场材料费') xcclfbm,
                        isnull(fff.费用金额,0) xcclf,
                            [dbo].[获取财务科目编码](a.出差类型,'吊装费') dzfbm,
                        isnull(ffffffffff.费用金额,0) dzf,
                        a.经理汇总 sfssfzr,
                        a.核算金额 cwhsbxje,
                        a.凭证编号 pzbh,
                        a.凭证创建时间 pzcjsj,
                        a.回传信息 hcxx
                        from X8060差旅费报销表 a
                left join (select taskid,sum(isnull(金额,0)) 金额 from X8060差旅费报销明细表1 group by taskid ) b on a.taskid=b.taskid
                left join (select taskid,sum(isnull(交通费,0)) 交通费 from X8060差旅费报销明细表2  group by taskid ) c on a.taskid=c.taskid
                left join (select taskid,sum(isnull(食宿费小计,0)) 食宿费小计 from X8060差旅费报销明细表3 group by taskid  ) d on a.taskid=d.taskid
                left join X8060差旅费报销明细表4 e on a.taskid=e.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='快递费' group by taskid )  fffffffff on a.taskid=fffffffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='现场材料费' group by taskid )  fff on a.taskid=fff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='改签退票费' group by taskid )  ffff on a.taskid=ffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='寄存费' group by taskid )  f on a.taskid=f.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='托运费' group by taskid )  fffff on a.taskid=fffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='护照费' group by taskid )  ffffff on a.taskid=ffffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='签证费' group by taskid )  fffffff on a.taskid=fffffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='员工聚餐' group by taskid )  ffffffff on a.taskid=ffffffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='吊装费' group by taskid )  ffffffffff on a.taskid=ffffffffff.taskid
                left join (           select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5 where 其他费用类型='招待费'  group by taskid ) ff on a.taskid=ff.taskid
                left join X8060差旅费报销明细表6 g on a.taskid=g.taskid
                left join ( select  taskid,max(ReceiveAt) ReceiveAt from BPMInstProcSteps where  NodeName='开始' group by taskid  ) h  on h.taskid= a.taskid
                left join ( select * from BPMInstProcSteps where NodeName='财务【办理】1' ) hh on hh.TaskID=a.taskid
                left join BPMInstTasks i on i.TaskID=a.taskid
                left join dv_BPMSysUsers3 j on a.报销人ID=j.account
                left join [192.168.1.208].[UFDATA_888_2019].[dbo].person k on k.cpersoncode=j.personid
                left join [192.168.1.208].[UFDATA_888_2019].[dbo].department l on l.cdepcode=k.cdepcode
                left join ( select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='其他' group by taskid  ) gg on a.taskid=gg.taskid
                where i.State in ('approved')
                group by h.ReceiveAt ,
                        a.报销人,
                    a.所属部门,
                        a.出差类型,
                        a.出差开始,
                        a.出差结束,
                        a.任务单号,
                        g.食宿费小计2 ,
                        e.国内补贴,
                        e.国外补贴,
                        e.多国补贴,
                        e.话费补贴,
                        e.误餐补贴,
                        ff.费用金额 ,
                        a.费用合计 ,
                        a.费用合计2 ,
                        a.经理汇总 ,
                        a.核算金额 ,
                        j.personid,
                        l.cdepcode ,
                        l.cdepname ,fffffffff.费用金额,
                        f.费用金额,fffffff.费用金额 ,ffff.费用金额,ffffffff.费用金额,fffff.费用金额,ffffff.费用金额,fff.费用金额,a.lsh ,
                        a.凭证编号,
                        a.凭证创建时间 ,
                        isnull(gg.费用金额,0),
                        a.回传信息, isnull(ffffffffff.费用金额,0),a.tmp2,hh.FinishAt
                                 ) t " . $where . " ORDER BY cwshsj "  . $order .  " OFFSET ? ROWS  FETCH NEXT ? ROWS ONLY ";
        // t.pzcjsj desc,t.sqsj desc,t.bxr desc,
        //wlog($sql, 1);

        //where t.RowNum >=0 and t.RowNum<=?
        //array ('page' => '1','rows' => '30')
        //$request->rows * $request->page
        $data = DB::connection('bpm')->select($sql, [$request->rows * ($request->page - 1), $request->rows * 1]);

        // $data = DB::connection('xmxkq')->select();
        //     [$request->rows * ($request->page - 1), $request->rows]
        // );
        //ROW_NUMBER() OVER (ORDER BY a.lsh) AS RowNum
        $sql2 = "SELECT count(*) c from (
                select convert(varchar(10),getdate(),120) zdrq,
                    '记' pzlb,
                    '0000' gdpzbh,
                    'deme' zdr,
                    a.lsh lsh,
                    h.ReceiveAt sqsj,
                    hh.FinishAt cwshsj,
                    j.personid gh,
                        a.报销人 bxr,
                        l.cdepcode bxrbmbm,
                        l.cdepname bxrbmmc,
                        a.出差类型 cclx,
                        a.出差开始 ccks,
                        a.出差结束 ccjs,
                        a.任务单号 rwdh,
                        a.tmp2  cwzzrwdh,
                        [dbo].[获取财务科目编码](a.出差类型,'城际交通费') gsclfbm,
                        sum(isnull(b.金额,0)) +
                        sum(isnull(c.交通费,0)) +
                        case when a.经理汇总='否'  then sum(isnull(d.食宿费小计,0))
                                else isnull(g.食宿费小计2,0) end +
                        isnull(e.国内补贴,0)+
                        isnull(e.国外补贴,0)+
                        isnull(e.多国补贴,0)+
                        isnull(e.误餐补贴,0)+
                        isnull(ffff.费用金额,0)+
                        isnull(f.费用金额,0)+isnull(fffffff.费用金额,0)+isnull(gg.费用金额,0) gsclf,
                        [dbo].[获取财务科目编码](a.出差类型,'话费补贴') hfbtbm,
                              isnull(e.话费补贴,0) hfbt,
                        [dbo].[获取财务科目编码](a.出差类型,'招待费') zdfbm,
                        isnull(ff.费用金额,0)+isnull(ffffffff.费用金额,0) zdf,
                        [dbo].[获取财务科目编码](a.出差类型,'快递费') yfbm,
                        isnull(fffff.费用金额,0)+isnull(fffffffff.费用金额,0)  yf,
                        [dbo].[获取财务科目编码](a.出差类型,'护照费') hzfbm,
                        isnull( ffffff.费用金额,0) hzf,
                        [dbo].[获取财务科目编码](a.出差类型,'现场材料费') xcclfbm,
                        isnull(fff.费用金额,0) xcclf,
                            [dbo].[获取财务科目编码](a.出差类型,'吊装费') dzfbm,
                        isnull(ffffffffff.费用金额,0) dzf,
                        a.经理汇总 sfssfzr,
                        a.核算金额 cwhsbxje,
                        a.凭证编号 pzbh,
                        a.凭证创建时间 pzcjsj,
                        a.回传信息 hcxx
                        from X8060差旅费报销表 a
                left join (select taskid,sum(isnull(金额,0)) 金额 from X8060差旅费报销明细表1 group by taskid ) b on a.taskid=b.taskid
                left join (select taskid,sum(isnull(交通费,0)) 交通费 from X8060差旅费报销明细表2  group by taskid ) c on a.taskid=c.taskid
                left join (select taskid,sum(isnull(食宿费小计,0)) 食宿费小计 from X8060差旅费报销明细表3 group by taskid  ) d on a.taskid=d.taskid
                left join X8060差旅费报销明细表4 e on a.taskid=e.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='快递费' group by taskid )  fffffffff on a.taskid=fffffffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='现场材料费' group by taskid )  fff on a.taskid=fff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='改签退票费' group by taskid )  ffff on a.taskid=ffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='寄存费' group by taskid )  f on a.taskid=f.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='托运费' group by taskid )  fffff on a.taskid=fffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='护照费' group by taskid )  ffffff on a.taskid=ffffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='签证费' group by taskid )  fffffff on a.taskid=fffffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='员工聚餐' group by taskid )  ffffffff on a.taskid=ffffffff.taskid
                left join (select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='吊装费' group by taskid )  ffffffffff on a.taskid=ffffffffff.taskid
                left join (           select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5 where 其他费用类型='招待费'  group by taskid ) ff on a.taskid=ff.taskid
                left join X8060差旅费报销明细表6 g on a.taskid=g.taskid
                left join ( select  taskid,max(ReceiveAt) ReceiveAt from BPMInstProcSteps where  NodeName='开始' group by taskid  ) h  on h.taskid= a.taskid
                left join ( select * from BPMInstProcSteps where NodeName='财务【办理】1' ) hh on hh.TaskID=a.taskid
                left join BPMInstTasks i on i.TaskID=a.taskid
                left join dv_BPMSysUsers3 j on a.报销人ID=j.account
                left join [192.168.1.208].[UFDATA_888_2019].[dbo].person k on k.cpersoncode=j.personid
                left join [192.168.1.208].[UFDATA_888_2019].[dbo].department l on l.cdepcode=k.cdepcode
                left join ( select taskid,sum(isnull(费用金额,0)) 费用金额 from X8060差旅费报销明细表5  where 其他费用类型='其他' group by taskid  ) gg on a.taskid=gg.taskid

                where i.State in ('approved')
                group by h.ReceiveAt ,
                        a.报销人,
                    a.所属部门,
                        a.出差类型,
                        a.出差开始,
                        a.出差结束,
                        a.任务单号,
                        g.食宿费小计2 ,
                        e.国内补贴,
                        e.国外补贴,
                        e.多国补贴,
                        e.话费补贴,
                        e.误餐补贴,
                        ff.费用金额 ,
                        a.费用合计 ,
                        a.费用合计2 ,
                        a.经理汇总 ,
                        a.核算金额 ,
                        j.personid,
                        l.cdepcode ,
                        l.cdepname ,fffffffff.费用金额,
                        f.费用金额,fffffff.费用金额 ,ffff.费用金额,ffffffff.费用金额,fffff.费用金额,ffffff.费用金额,fff.费用金额,a.lsh ,
                        a.凭证编号,
                        a.凭证创建时间 ,
                        isnull(gg.费用金额,0),
                        a.回传信息, isnull(ffffffffff.费用金额,0),a.tmp2,hh.FinishAt
                                 ) t" . $where;

        $total = DB::connection('bpm')->select($sql2)[0]->c;

        $mgid = Session::get("mgid");
        Log::info("用户" . $mgid . "点击了查询按钮");
        return ["rows" => $data, "total" => $total];
    }

    function khgzlist($request)
    {
        // TODO: Implement khgzlist() method.
        $where = " where 1=1 ";
        if ($request->gid) {
            $where .= " and gid= '$request->gid' ";
        }
        if (!$request->sort) {
            $request->sort = "create_time";
        }
        if (!$request->order) {
            $request->order = "desc";
        }
        $data = DB::select(
            "SELECT
        g.id,g.gjnr,g.gjr,g.gid,g.create_time,g.valid,g.khqm,g.khjc,g.mid,g.khjz
                            FROM zhyl_sys_goodprocess g
                            " . $where . "order by $request->sort $request->order LIMIT ?,? ",
            [$request->rows * ($request->page - 1), $request->rows]
        );
        $total = DB::select("SELECT count(*) c FROM zhyl_sys_goodprocess g")[0]->c;
        return ["rows" => $data, "total" => $total];
    }


    function addgood($request)

    {

        // TODO: Implement addgood() method.

        //$t = new \Tools();

        //$info = DB::select("select * from zhyl_sys_manager where mg_id=?",[Session::get('mgid')]);

        $info = \Tools::getManagerInfo();

        //$hashids          = new Hashids('', 6);

        //$info->account    = $hashids->encode($id);

        $data = array(

            "id" => Uuid::uuid1(),

            "goodname" => $request->goodname,

            "price" => $request->price,

            "score" => $request->score,

            //"goodcontent" => $request->editorValue,

            "repertory" => $request->repertory,

            "imgurl" => $request->imgurl,

            "modelfiles" => $request->modelfiles,

            "singleimg" => 'storage/uploads/' . $request->singleimg,

            "stick" => $request->stick,

            "ssdl" => $request->ssdl,

            "sszl" => $request->sszl,

            "dg" => $request->dg,

            "cztt" => $request->cztt,

            "fg" => $request->fg,

            "ms" => $request->ms,

            "creater" => \Tools::getManagerInfo()->mg_code, //$request->session()->get("userinfo")[0]->mg_name,

            "createtime" => time(),

            "gcode" => uniqid()

        );

        if ($request->goodid) {

            //更新

            DB::table("goodslist")->where("id", $request->goodid)->update($data);
        } else {

            //插入

            DB::table("goodslist")->insert($data);
        }
    }


    function removegood($request)
    {

        // TODO: Implement removegood() method.

    }


    function detailgood($request)
    {

        // TODO: Implement detailgood() method.
        return DB::table("goodslist")->find($request->id);
    }


    /**
     * 商品上架
     */

    function goodsjxj($request)
    {

        //$userinfo = \Tools::getManagerInfo();//$request->session()->get("userinfo")[0];

        if ($request->param == 1) {

            //上架

            DB::table('goodslist')->where("id", $request->id)->update(array("sffb" => 1, 'fbr' => \Tools::getManagerInfo()->mg_name, 'fbsj' => time()));
        } else if ($request->param == 2) {

            //下架

            DB::table('goodslist')->where("id", $request->id)->update(array("sffb" => 2, 'deleter' => \Tools::getManagerInfo()->mg_name, 'deletetime' => time()));
        }

        return "success";
    }


    /**
     * @param $request
     * 门店列表
     */

    function shoplist($request)

    {

        // TODO: Implement shoplist() method.

        $where = " where 1=1 ";

        if ($request->shopname) {

            $where .= " and s.shopname like '%$request->shopname%'";
        }

        if ($request->zprs) {

            $where .= " and s.zprs like '%$request->zprs%'";
        }

        if ($request->valid) {
            $where .= " and s.valid = '$request->valid'";
        }

        if ($request->lxdh) {
            $where .= " and s.lxdh like '%$request->lxdh%'";
        }

        return DB::select(
            "select s.* from zhyl_sys_shoplist s " . $where . "LIMIT ?,?",
            [$request->rows * ($request->page - 1), $request->rows]
        );
    }

    /**
     * @param $request 删除客户信息
     */
    function deleteshop($request)
    {

        $ups['sfyx'] = $request->valid;

        $ups['gxsj'] = date("Y-m-d H:i:s", time());
        $ids[] = explode(",", $request->ids);
        $where[] = [function ($query) use ($ids) {
            $query->whereIn('id', $ids[0]);
        }];
        DB::table("goodslist")->where($where)->update($ups);
    }



    function add_update_shop($request)
    {
        //TODO: Implement add_update_shop() method.
        $myid = Session::get("mgid"); //跟进人id
        $myinfo = DB::select("select * from zhyl_sys_manager where mg_id = $myid limit 1");

        $data = array(
            "khqm" => $request->khqm,
            "khjc" => $request->khjc,
            "gjnr" => $request->gjnr,
            "gjr" => $myinfo[0]->mg_name,
            "gid" => $request->id, //客户id
            "mid" => $myid, //跟进人id
            "khjz" => $request->khjz, //跟进人id
            "valid" => "有效",
            "create_time" => date("Y-m-d H:i:s", time())
        );
        //添加跟进记录--子表
        DB::table("goodprocess")->insert($data);
        $where['id'] = $request->id;
        $ups['zhgjr'] = $myinfo[0]->mg_name;
        $ups['khjz'] = $request->khjz;
        $ups['zhlxsj'] = date("Y-m-d H:i:s", time()); //应为没有固定电话，所以最后联系时间只能以添加系统数据时间为准
        DB::table("goodslist")->where($where)->update($ups);
    }

    //新增客户--手动新增
    function addkh($request)
    {
        $data = array(
            "khqm" => $request->khqm,
            "khjc" => $request->khjc,
            "lxr" => $request->lxr,
            "xb" => $request->xb,
            "shr" => $request->shr,
            "clrq" => $request->clrq,
            "zw" => $request->zw,
            "wx" => $request->wx,
            "qq" => $request->qq,
            "sj" => $request->sj,
            "zj" => $request->zj,
            "yx" => $request->yx,
            "gzdw" => $request->gzdw,
            "wz" => $request->wz,
            "dz" => $request->dz,
            "bz" => $request->bz,
            "gj" => $request->gj,
            "sheng" => $request->sheng,
            "shi" => $request->shi,
            "chs" => $request->chs,
            "qx" => $request->qx,
            "hy" => $request->hy,
            "chz" => $request->chz,
            "ly" => $request->ly,
            "bkxm" => $request->bkxm,
            "sfjqqhy" => $request->sfjqqhy,
            "sfjjkhyl" => $request->sfjjkhyl,
            //"khjz" => $request->khjz,
            "khlx" => $request->khlx,
            "khxj" => $request->khxj,
            "khlx" => $request->khlx,
            "khjz" => '新入库',
            "sfyx" => '是',
            "cjsj" => date("Y-m-d H:i:s", time())
        );

        DB::table("goodslist")->insert($data);
    }


    /**
     * 门店详情
     */

    public function shopdetail($request)

    {

        //$info = DB::table("shoplist")->find($request->id);

        return DB::table("shoplist")->find($request->id);
    }


    function recordlist($request)

    {

        // TODO: Implement recordlist() method.

        $where = "  where 1=1 ";

        if ($request->dhr) {

            $where .= " and dhr like '%$request->dhr%' ";
        }

        if ($request->lpmc) {

            $where .= " and lpmc like '%$request->lpmc%' ";
        }

        if ($request->sjh) {

            $where .= " and sjh like '%$request->sjh%' ";
        }

        if ($request->thfs) {

            $where .= " and thfs like '%$request->thfs%' ";
        }

        if ($request->fhzt) {

            $where .= " and fhzt like '%$request->fhzt%' ";
        }

        if ($request->wldh) {

            $where .= " and wldh like '%$request->wldh%' ";
        }

        if ($request->ffr) {

            $where .= " and ffr like '%$request->ffr%' ";
        }

        if ($request->ffmd) {

            $where .= " and ffmd like '%$request->ffmd%' ";
        }

        if ($request->valid) {

            $where .= " and valid =$request->valid-1 ";
        }

        if ($request->stime && $request->etime) {

            $stime = strtotime($request->stime);

            $etime = strtotime($request->etime);

            $where .= " and dhsj between $stime and $etime ";
        }

        $data = DB::select(
            "select *,FROM_UNIXTIME(dhsj,'%Y-%m-%d') fdhsj,if(valid=1,'有效','无效')

                             valid from zhyl_sys_recordlist " . $where . "LIMIT ?,?",

            [$request->rows * ($request->page - 1), $request->rows]
        );

        $total = DB::select("select count(*) c from zhyl_sys_recordlist" . $where)[0]->c;

        return ["rows" => $data, "total" => $total];
    }


    /**
     * @param $request
     * 更新物流信息
     */

    function savewldh($request)

    {

        // TODO: Implement savewldh() method.

        DB::transaction(function () use ($request) {

            DB::table("recordlist")->where("id", $request->id)->update(array("wldh" => $request->wldh));

            $this->addlogs(getSession($request)->mg_code, time(), '更新物流单号信息', $request->wldh);

            //请求微信接口向特定用户在微信公众号上发送推送消息

        });
    }


    function dhjlzf($request)

    {

        // TODO: Implement dhjlzf() method.

        DB::transaction(function () use ($request) {

            DB::table("recordlist")->where("id", $request->id)->update(array("valid" => 0));

            if ($request->wldh) {

                $repertory = DB::table("goodslist")->find($request->gid)->repertory + $request->dhsl;

                DB::table("goodslist")->where("id", $request->gid)->update(array("repertory" => $repertory));
            }

            $this->addlogs(getSession($request)->mg_code, time(), '兑换记录作废', $request->wldh);
        });
    }


    /**
     * @param $request
     * @param $czr    操作人
     * @param $czsj   操作时间
     * @param $czyy   操作原因
     * @param $value  操作值
     * 新增日志
     */

    function addlogs($czr = null, $czsj = null, $czyy = null, $value = null, $czyyxq = null)
    {

        return (DB::table("logs")->insert(array("czr" => $czr, "czsj" => $czsj, "czyy" => $czyy, "value" => $value, "czyyxq" => $czyyxq)))

            ? true : false;
    }


    function getChildren($request)

    {

        // TODO: Implement getChildren() method.

        return DB::table("sszl")->where("pid", $request->pid)->get();
    }
}
